﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Warehouse_Management.Import;
using Warehouse_Management.New;
using DevExpress;

namespace Warehouse_Management
{
    public partial class frmDanhMucHangHoa : DevExpress.XtraEditors.XtraForm
    {
        public frmDanhMucHangHoa()
        {
            InitializeComponent();
        }
        PhieuGD PGD = new PhieuGD();
        public IList<Hang> listHang;
        private DataTable dt;
        public int status = 0;
        private void frmNhapManager_Load(object sender, EventArgs e)
        {
            loadData();
            BindingData();
            
        }
        private void loadData()
        {
            //listHang = Hang.SelectCollectionAll();
            dt = Hang.SelectAll().Tables[0];
        }
        private void BindingData()
        {
//             ListBinding = new BindingList<Hang>(listHang);
//             ListBinding.AllowEdit = ListBinding.AllowNew = ListBinding.AllowRemove = true;
//             ListBinding.ListChanged += new ListChangedEventHandler(ListBinding_ListChanged);
//             gridControl1.DataSource = ListBinding;
            gridControl1.DataSource = dt;
        }
//         public void TaoMoi()
//         {
//             frmHang f = new frmHang();
//             f.ShowDialog(this);
//         }
//         public void Edit()
//         {
//             frmHang f = new frmHang();
//             f.ShowDialog(this);
//         }
        public void import()
        {
            frmImportDanhMuc import = new frmImportDanhMuc();
            import.dtHangHoa = dt;
            import.ShowDialog(this);
            for (int i = 0; i < import.listHangNew.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["Barcode"] = import.listHangNew[i].Barcode;
                dr["TenHang"] = import.listHangNew[i].TenHang;
                dr["DVT_ID"] = import.listHangNew[i].DVT_ID;
                dr["GhiChu"] = import.listHangNew[i].GhiChu;
                dt.Rows.Add(dr);
            }
        }
        public void Save()
        {
            try
            {
                for (int i =0; i < dt.Rows.Count;i++)
                {
                    DataRow dr = dt.Rows[i];
                    if (dr.RowState != DataRowState.Unchanged)
                    {
                        Hang h = GetHang(i);
                        if (h != null)
                        {
                            if (dr.RowState == DataRowState.Deleted)
                                h.Delete();
                            else if (dr.RowState == DataRowState.Added || dr.RowState == DataRowState.Modified)
                                h.InsertUpdate();
                        }
                        
                    }
                }
                MessageBox.Show("Lưu thành công");
                this.Text = "Danh mục hàng hóa";
                try
                {
                    gridView1.RefreshData();
                    dt.AcceptChanges();
                }
                catch 
                {
                	
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi khi lưu thông tin danh mục hàng hóa. " + ex.Message);
 

            }
        }

//         private void ListBinding_ListChanged(object sender, ListChangedEventArgs e)
//         {
//             switch (e.ListChangedType)
//             {
//                 case ListChangedType.ItemAdded:
//                     indexAdd.Add(e.NewIndex);
//                     break;
//                 case ListChangedType.ItemChanged:
//                     indexEdit.Add(e.NewIndex);
//                     break;
//                 case ListChangedType.ItemDeleted:
//                     indexRemove.Add(e.NewIndex);
//                     break;
//             }
//         }


        private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.ToString() =="Đơn vị tính")
                if(e.Value != null)
                    e.DisplayText = Global.getDVTByMa(e.Value.ToString());
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hit =  gridView1.CalcHitInfo((e as MouseEventArgs).Location);
            if (hit.InRow)
            {
                gridView1.SelectRow(hit.RowHandle);
                this.EditHang();
            }
        }

        public void EditHang()
        {
            int RowHandle = gridView1.GetSelectedRows()[0];
            if (RowHandle >= 0)
            {
                int index = gridView1.GetDataSourceRowIndex(RowHandle);
                Hang hang = GetHang(index);
                if (hang != null)
                {
                    bool hangEdit = true;
                    string Barcode = hang.Barcode;
                    while (hangEdit)
                    {
                        frmHang f = new frmHang();
                        f.hang = hang;
                        f.type = frmHang.OpenType.IsNew;
                        f.ShowDialog(this);
                        if (f.isSave)
                        {
                            hang = f.hang;
                            if (hang.Barcode == Barcode || CheckHang(hang.Barcode))
                            {
                                DataRow dr = dt.Rows[index];
                                dr["Barcode"] = hang.Barcode;
                                dr["TenHang"] = hang.TenHang;
                                dr["DVT_ID"] = hang.DVT_ID;
                                dr["GhiChu"] = hang.GhiChu;
                                hangEdit = false;
                                this.Text = "Danh mục hàng hóa" + "(*)";
                            }
                            else
                            {
                                hangEdit = MessageBox.Show("Mã hàng (barcode) đã tồn tại, Bạn có muốn nhập lại ?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes;
                            }
                        }
                        else
                            hangEdit = false;
                    }


                }
            }
       
        }
        
        public void NewHang()
        {
            Hang hang = new Hang();
            bool hangnew = true;
            try
            {
                while (hangnew)
                {
                    frmHang f = new frmHang();
                    f.hang = hang;
                    f.type = frmHang.OpenType.IsNew;
                    f.ShowDialog(this);
                    if (f.isSave)
                    {
                        hang = f.hang;
                        if (CheckHang(hang.Barcode))
                        {
                            DataRow dr = dt.NewRow();
                            dr["Barcode"] = hang.Barcode;
                            dr["TenHang"] = hang.TenHang;
                            dr["DVT_ID"] = hang.DVT_ID;
                            dr["GhiChu"] = hang.GhiChu;
                            dt.Rows.Add(dr);
                            hangnew = false;
                            this.Text = "Danh mục hàng hóa" + "(*)";
                        }
                        else
                        {
                            hangnew = MessageBox.Show("Mã hàng (barcode) đã tồn tại, Bạn có muốn nhập lại ?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes;
                        }
                    }
                    else
                        hangnew = false;
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
          
           
            
        }
        
        public void DeleteHang()
        {
            int[] rowsHandle = gridView1.GetSelectedRows();
            for (int i = 0; i < rowsHandle.Length; i++)
            {
                dt.Rows.RemoveAt(gridView1.GetDataSourceRowIndex(rowsHandle[i]));
            }
            if (rowsHandle.Length > 0)
                this.Text = "Danh mục hàng hóa" + "(*)";
        }
        
        private bool CheckHang(string barcode)
        {
            DataRow[] dr = dt.Select("Barcode = '" + barcode + "'");
            if (dr.Length > 0) return false;
            else
                return true;
        }
        
        private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                DataRow dr = gridView1.GetDataRow(e.RowHandle);
                if (dr != null)
                {
                    switch (dr.RowState)
                    {
                        case DataRowState.Added:
                            e.Appearance.BackColor = Color.Green;
                            break;
                        case DataRowState.Modified:
                            e.Appearance.BackColor = Color.Yellow;
                            break;
                        case DataRowState.Deleted:
                            e.Appearance.BackColor = Color.Red;
                            break;
                    }
                }

            }
        }

        private Hang GetHang(int index)
        {
            Hang hanghoa = new Hang();
            DataRow dr = dt.Rows[index];
            if (dr != null)
            {
                hanghoa.Barcode = dr["Barcode"].ToString();
                hanghoa.DVT_ID = dr["DVT_ID"].ToString();
                hanghoa.GhiChu = dr["GhiChu"].ToString();
                hanghoa.TenHang = dr["TenHang"].ToString();
                return hanghoa;
            }
            else
                return null;
        }

        private void frmDanhMucHangHoa_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.Text.Contains("(*)"))
                if (MessageBox.Show("Bạn có muốn lưu thông tin trước khi đóng ? ", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    this.Save();

        }
      
    }
}